package cpb.module.evaluation.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.aries.ui.view.title.TitleBarView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import cpb.module.evaluation.EvaluationPath;
import cpb.module.evaluation.R;
import cpb.module.evaluation.adapter.OptionListAdapter;
import cpb.module.evaluation.entity.Topic;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.entity.BaseTResp;
import tech.com.commoncore.utils.ToastUtil;

/**
 * Author:Cpb
 * Time  : 2019-08-06
 * Desc  : 答题页面
 */
@Route(path = EvaluationPath.EXAM)
public class ExamActivity extends BaseTitleActivity {

    private TextView mTvPos;
    private TextView mTvTopicTitle;
    private RecyclerView mRvTopicOption;
    private Button mBtnNext;

    private Topic topic;
    private int pos;
    private OptionListAdapter mAdapter;
    private RecyclerView.AdapterDataObserver observer;

    @Override
    public void beforeSetContentView() {
        super.beforeSetContentView();
        topic = (Topic) getIntent().getSerializableExtra("topic");
        pos = getIntent().getIntExtra("pos", 0);
    }

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("风险评测").setBackground(getResources().getDrawable(R.drawable.bg_titlebar_jb));
    }

    @Override
    public int getContentLayout() {
        return R.layout.evaluation_activity_exam;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mTvPos = findViewById(R.id.tv_pos);
        mTvTopicTitle = findViewById(R.id.tv_topic_title);
        mRvTopicOption = findViewById(R.id.rv_topic_option);
        mBtnNext = findViewById(R.id.btn_next);

        mRvTopicOption.setLayoutManager(new GridLayoutManager(mContext, 2));
        mAdapter = new OptionListAdapter();
        mRvTopicOption.setAdapter(mAdapter);

        mTvPos.setText(pos + "/21");
        mTvTopicTitle.setText(topic.title);
        mAdapter.setNewData(topic.option);

        if (pos == 21) {
            mBtnNext.setText(R.string.evaluation_exam_btn_2);
        } else {
            mBtnNext.setText(R.string.evaluation_exam_btn_1);
        }

        initEvent();

        refreshBtn();

    }

    private void refreshBtn() {
        if (mAdapter.getSelectIndex() == -1) {
            mBtnNext.setEnabled(false);
        } else {
            mBtnNext.setEnabled(true);
        }
    }

    private void initEvent() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mAdapter.updateSelect(position);
            }
        });

        observer = new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                refreshBtn();
            }
        };

        mAdapter.registerAdapterDataObserver(observer);

        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pos == 21) {
                    loadExamResult();
                } else {
                    loadNextTopic();
                }
            }
        });
    }

    private void loadNextTopic() {
        showLoading(getString(R.string.evaluation_tip_loading));
        ViseHttp.GET("yapi/risk/index")
                .addParam("page", pos + "")
                .request(new ACallback<BaseTResp<Topic>>() {
                    @Override
                    public void onSuccess(BaseTResp<Topic> data) {
                        hideLoading();
                        if (1 == data.code && data.data != null) {
                            Intent intent = new Intent(mContext, ExamActivity.class);
                            intent.putExtra("topic", data.data);
                            intent.putExtra("pos", pos + 1);
                            startActivity(intent);
                            finish();
                        } else {
                            ToastUtil.show(R.string.evaluation_tip_load_fail);
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        hideLoading();
                        ToastUtil.show(R.string.evaluation_tip_load_fail);
                    }
                });


    }

    private void loadExamResult() {
        showLoading(getString(R.string.evaluation_tip_loading_result));
        ViseHttp.GET("yapi/risk/index")
                .addParam("page", "22")
                .request(new ACallback<BaseTResp<String>>() {
                    @Override
                    public void onSuccess(BaseTResp<String> data) {
                        hideLoading();
                        if (1 == data.code && data.data != null) {
                            ARouter.getInstance().build(EvaluationPath.RESULT).withString("result", data.data).navigation();
                            finish();
                        } else {
                            ToastUtil.show(R.string.evaluation_tip_load_result_fail);
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        hideLoading();
                        ToastUtil.show(R.string.evaluation_tip_load_result_fail);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAdapter.unregisterAdapterDataObserver(observer);
    }
}
